#!/usr/bin/python

import time, sys


""" 
Problem 17

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.

"""

def get_string(num, numbers):
    word = ''

    if num == 1000:
        word = 'onethousand'
        return word
    if num < 20:
        word = numbers[num]
        return word
    
    number = str(num) 
    if len(number) == 3:
        hundreds = int(number[0])
        word = numbers[hundreds] + 'hundred'
        if num % 100 == 0:
            return word
        else:
            word = word + 'and'
            number = number[1:]
    if num % 10 == 0 or int(number) < 20:
        num = int(number)
        word = word + numbers[num]
        return word
    else:
        tens = number[0]
        tens = int(tens) * 10
        units = int(number[1])
        tens = numbers[tens] 
        units = numbers[units]    
        return word + tens + units



def main():
    start = time.time()

    # setup of number values

    units = [ (1, 'one'), (2, 'two'), (3, 'three'), (4, 'four'), (5, 'five'), 
            (6, 'six'), (7, 'seven'), (8, 'eight'), (9, 'nine'), (10, 'ten'), 
            (11, 'eleven'), (12, 'twelve'), (13, 'thirteen'), (14, 'fourteen'), 
            (15, 'fifteen'), (16, 'sixteen' ), (17, 'seventeen'), 
            (18, 'eighteen'), (19, 'nineteen') ]

    tens = [ (20, 'twenty'), (30, 'thirty'), (40, 'forty'), (50, 'fifty'), 
            (60, 'sixty'), (70, 'seventy'), (80, 'eighty'), (90, 'ninety'), ]
    
    numbers = {}
    for key, num in units:      
        numbers[key] = num
    for key, num in tens:
        numbers[key] = num
        
    total = 0

    for num in xrange(1, 1001):
        word = get_string(num, numbers)
        total += len(word)
        print num, word, len(word)
    print total

    print '{0:.2f} s'.format(time.time() - start)  

# solution: 21114 in 0.01 s
    
    
if __name__ == '__main__':
    main()
